﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRMEDIASTOREManager : BasePage
{
    public static bool Insert;
    BUS_WRMEDIASTORE busWRMEDIASTORE = new BUS_WRMEDIASTORE();
    BUS_WRENTRY busWrentry = new BUS_WRENTRY();
    BUS_WRMEDIATYPE busWrmediatype = new BUS_WRMEDIATYPE();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }

    private void BindGrid()
    {
        grvWRMEDIASTORE.DataSource = busWRMEDIASTORE.Get_MediaStore_List();
        //grvWRMEDIASTORE.DataSource = busWRMEDIASTORE._GetList().Select(p=>new
        //{
        //    p.ID,
        //    p.Active,
        //    p.Keywords,
        //    p.MediaPath,
        //    UpdateDate = Convert.ToDateTime(p.UpdateDate).ToShortDateString(),
        //    Name = busWruser._GetObject(Convert.ToInt32(p.IDUpdateBy)).Lastname + " " + busWruser._GetObject(Convert.ToInt32(p.IDUpdateBy)).Firstname,
        //    EntryName = busWrentry._GetObject(Convert.ToInt32(p.IDEntry)).EntryName,
        //    MediaTypeName = busWrmediatype._GetObject(Convert.ToInt32(p.IDMedia)).MediaTypeName
        //}).ToList();
        grvWRMEDIASTORE.DataBind();
    }

    public void BindDrop()
    {
        dropIDEntry.DataSource = busWrentry._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.EntryName }).ToList();
        dropIDEntry.DataTextField = "EntryName";
        dropIDEntry.DataValueField = "Id";
        dropIDEntry.DataBind();
        //dropIDMedia.DataSource = busWrmediatype._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.MediaTypeName }).ToList();
        //dropIDMedia.DataTextField = "MediaTypeName";
        //dropIDMedia.DataValueField = "Id";
        //dropIDMedia.DataBind();
        string lang = Request.Cookies["SelLang"].Value;
        if (lang == "vi")
        {
            dropIDMedia.DataSource =
                busWrmediatype._GetList()
                    .Where(q => q.Active == "Y" && q.IDLang == 2)
                    .Select(p => new { p.ID, p.MediaTypeName })
                    .ToList();
            dropIDMedia.DataTextField = "MediaTypeName";
            dropIDMedia.DataValueField = "ID";
            dropIDMedia.DataBind();
        }
        else
        {
            dropIDMedia.DataSource = busWrmediatype._GetList().Where(q => q.Active == "Y" && q.IDLang == 1).Select(p => new { p.ID, p.MediaTypeName }).ToList();
            dropIDMedia.DataTextField = "MediaTypeName";
            dropIDMedia.DataValueField = "ID";
            dropIDMedia.DataBind();
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtMediaName.Text = "";
        txtMediaPath.Text = "";
        txtDescription.Text = "";
        txtKeywords.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRMEDIASTORE.Items.Count > 0)
        {
            for (int i = 0; i < grvWRMEDIASTORE.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRMEDIASTORE.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRMEDIASTORE.Items[i].Cells[1].Text;
                    try
                    {
                        busWRMEDIASTORE._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtMediaName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập MediaName!");
            txtMediaName.Focus();
            return;
        }
        if (txtMediaPath.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập MediaPath!");
            txtMediaPath.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRMEDIASTORE obj = new WRMEDIASTORE();
        
        obj.IDEntry = Convert.ToInt32(dropIDEntry.SelectedValue);
        obj.IDMedia = Convert.ToInt32(dropIDMedia.SelectedValue);
        obj.MediaName = txtMediaName.Text;
        obj.MediaPath = txtMediaPath.Text;
        obj.Description = txtDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        if (Insert)
        {
            obj.IDCreateBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRMEDIASTORE._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRMEDIASTORE._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRMEDIASTORE_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRMEDIASTORE _WRMEDIASTORE = busWRMEDIASTORE._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            try
            {
                dropIDEntry.SelectedValue = _WRMEDIASTORE.IDEntry.ToString();
            }
            catch
            {
                Show("Entry is not active!");
            }
            try
            {
                dropIDMedia.SelectedValue = _WRMEDIASTORE.IDMedia.ToString();
            }
            catch
            {
                Show("Media Store is not active!");
            }
            txtMediaName.Text = _WRMEDIASTORE.MediaName;
            txtMediaPath.Text = _WRMEDIASTORE.MediaPath;
            txtDescription.Text = _WRMEDIASTORE.Description;
            dropActive.SelectedValue = _WRMEDIASTORE.Active.ToString();
            txtKeywords.Text = _WRMEDIASTORE.Keywords;

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRMEDIASTORE._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRMEDIASTORE_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRMEDIASTORE.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
